package zuel.innovation.disley.wishwall.controller;

import cn.fabrice.common.constant.BaseConstants;
import cn.fabrice.common.pojo.BaseResult;
import cn.fabrice.common.pojo.DataResult;
import cn.fabrice.jfinal.annotation.Param;
import cn.fabrice.jfinal.annotation.ValidateParam;

import cn.fabrice.jfinal.ext.cros.interceptor.CrossInterceptor;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import zuel.innovation.disley.common.module.Project;
import zuel.innovation.disley.common.module.User;
import zuel.innovation.disley.common.module.Wishwall;
import zuel.innovation.disley.interceptor.AuthInterceptor;
import zuel.innovation.disley.wishwall.service.WishwallService;
import zuel.innovation.disley.wishwall.service.WishwallSessionService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 768
 */
@Before(CrossInterceptor.class)
@Path("/wishwall")
@ValidateParam


public class WishwallController extends Controller {
    @Inject
    WishwallService service;
    @Inject
    WishwallSessionService sessionService;

    /**
     * 获取当前已有的祝福墙信息
     */

    @Clear(AuthInterceptor.class)
    public void getWishInfo() {
        Wishwall wishwall = service.get("getWishInfo");

        renderJson(DataResult.data(wishwall));
    }


    /**
     * 新增祝福
     *
     * @param wisher  祝福者
     * @param toWish  想要祝福的人
     * @param wishContent 祝福内容
     *
     * @throws ParseException
     */
    @Clear(AuthInterceptor.class)
    @Param(name = "wisher", required = true)
    @Param(name = "toWish", required = true)
    @Param(name="wishContent",required = true)

    public void addNewWish(String wisher, String toWish,String wishContent)throws ParseException{

        Wishwall wishwall = new Wishwall();

        wishwall.setWisher(wisher);
        wishwall.setToWish(toWish);
        wishwall.setWishContent(wishContent);

        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date wishCreate = df.parse(df.format(System.currentTimeMillis()));
        wishwall.setCreatedTime(wishCreate);


        renderJson(wishwall.save() ? DataResult.data(wishwall) : DataResult.fail("添加失败"));
    }


    /**
     * 用户修改祝福内容
     *
     * @param wisher  祝福者
     * @param toWish  想要祝福的人
     * @param wishContent 祝福内容
     */
    @Clear(AuthInterceptor.class)
    @Param(name="wishId",required = true)
    @Param(name = "wisher", required = true)
    @Param(name = "toWish", required = true)
    @Param(name="wishContent",required = true)

    public void updateWish(int wishId,String wisher, String toWish,String wishContent)throws ParseException {
        Kv cond = Kv.by("wishId", wishId);
        Wishwall wishwall = service.get(cond, "getWishById");
        if (wishwall == null) {
            renderJson(BaseResult.fail("该祝福不存在"));
        } else {
            wishwall.setWisher(wisher);
            wishwall.setToWish(toWish);
            wishwall.setWishContent(wishContent);
            //获取当前时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            Date wishUpdate = df.parse(df.format(System.currentTimeMillis()));
            wishwall.setUpdatedTime(wishUpdate);
            renderJson(wishwall.update() ? DataResult.data(wishwall) : DataResult.fail("修改失败"));
        }
    }

}
